//
//  FruitOrderFinishDetailsVC.m
//  HuiMWorld
//
//  Created by chirui on 2018/3/26.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "FruitOrderFinishDetailsVC.h"
#import "OrderTableViewCell.h"

@interface FruitOrderFinishDetailsVC ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
}

@end

@implementation FruitOrderFinishDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"订单详情")
    
    NSLog(@"dataDic==%@",self.dataDic);
    
    [self creatTableView];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_return_back" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[OrderTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[OrderTableViewCell reuseIdentifier]];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    NSArray * array = self.dataDic[@"product"];
    
    return array.count+1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray * array = self.dataDic[@"product"];
    
    if (indexPath.section==array.count) {
        return 0.01;
    }else {
        
        return 100;
    }
 
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section==0) {
          return 210;
    }else {
        
        return 0.01;
    }
  
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    

    NSArray * array = self.dataDic[@"product"];
    
    if (section==array.count) {
        return 400;
    }else {
    NSDictionary * dic = array[section];
    UIView * commentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    commentView.backgroundColor = [UIColor whiteColor];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)];
    label.text = @"评论内容";
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:15];
    [commentView addSubview:label];
    
    UILabel * comment = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, WIDTH - 110, 20)];
    comment.text = dic[@"comment"];
    comment.textColor = [UIColor blackColor];
    comment.font = [UIFont systemFontOfSize:15];
    comment.numberOfLines = 0;
    [comment sizeToFit];
    [commentView addSubview:comment];
    
        if (comment.text.length>0) {
            commentView .frame = CGRectMake(0, 0, WIDTH, CGRectGetMaxY(comment.frame)+10);
        }else {
            
            commentView .frame = CGRectMake(0, 0, WIDTH,40);
        }

    
    UIView * replyView;
 
        replyView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(commentView.frame)+10, WIDTH, 50)];
    
    replyView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    UILabel * reply = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)];
    reply.text = @"平台回复";
    reply.textColor = MyTabbarClick;
    reply.font = [UIFont systemFontOfSize:15];
    [replyView addSubview:reply];
    
    UILabel * replyVolu = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, WIDTH - 110, 20)];
    replyVolu.text = dic[@"replay"];
    replyVolu.textColor = [UIColor grayColor];
    replyVolu.font = [UIFont systemFontOfSize:15];
    replyVolu.numberOfLines = 0;
    [replyVolu sizeToFit];
    [replyView addSubview:replyVolu];
    
        if (replyVolu.text.length>0) {
             replyView.frame = CGRectMake(0, CGRectGetMaxY(commentView.frame)+10, WIDTH, CGRectGetMaxY(replyVolu.frame)+10);
        }else{
            
             replyView.frame = CGRectMake(0, CGRectGetMaxY(commentView.frame)+10, WIDTH, 40);
        }
       
        return CGRectGetMaxY(replyView.frame);;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray * array = self.dataDic[@"product"];
    if (indexPath.section==array.count) {
        static NSString * cellId = @"cellId";
        UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId ];
        if (cell==nil) {
            
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
        }else {
            while ([cell.contentView.subviews lastObject]!=nil) {
                
                [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
            }
        }
        return cell;
        
        
    }else {
        
        NSDictionary * dic = array[indexPath.section];
        OrderTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[OrderTableViewCell reuseIdentifier] forIndexPath:indexPath];
        
        [cell.goodsImage sd_setImageWithURL:ImageUrl(dic[@"cover_photo"]) placeholderImage:ImageName(@"")];
        cell.goodsName.text = dic[@"product_name"];
        cell.goodsName.frame = CGRectMake(98, 10, WIDTH -110, 20);
        cell.goodsName.numberOfLines = 2;
        [cell.goodsName sizeToFit];
        cell.priceLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"price"]];
        cell.numberLabel.text = [NSString stringWithFormat:@"X%@",dic[@"number"]];
        cell.guigeLabel.text = [NSString stringWithFormat:@"%@",dic[@"option_name"]];
        return cell;
    }

    
    
  
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section==0) {
        
    UIView * heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 210)];
    heardView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    
    UIView * statisView =[[UIView alloc]initWithFrame:CGRectMake((WIDTH-100)/2, 30, 100, 40)];
    statisView.backgroundColor = [UIColor clearColor];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 30, 30)];
    
    [statisView addSubview:imageView];
    
    if ([self.dataDic[@"status"] isEqualToString:@"unpay"]) {
        
        imageView.image = ImageName(@"icon_user_order_payment_de");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor redColor] andText:@"未支付" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    if ([self.dataDic[@"status"] isEqualToString:@"payed"]) {
        imageView.image = ImageName(@"icon_user_order_pd_de");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor redColor] andText:@"待发货" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    if ([self.dataDic[@"status"] isEqualToString:@"deliver"]) {
        imageView.image = ImageName(@"icon_user_order_gr");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor redColor] andText:@"待收货" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    if ([self.dataDic[@"status"] isEqualToString:@"wait_comment"]) {
        imageView.image = ImageName(@"icon_user_order_evaluated");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor redColor] andText:@"待评价" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    if ([self.dataDic[@"status"] isEqualToString:@"done"]) {
        imageView.image = ImageName(@"icon_user_order_ok");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor redColor] andText:@"已完成" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    UIView * addressView =[[UIView alloc]initWithFrame:CGRectMake(0, 100, WIDTH, 100)];
    addressView.backgroundColor =[UIColor whiteColor];
    
    
    UIImageView * addressImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 75/2, 25, 25)];
    addressImageView.image = ImageName(@"icon_order_add");
    [addressView addSubview:addressImageView];
    
    [self creatLabelFrame:CGRectMake(35, 20, 120, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"data_address"][@"fullname"] andFont:15 andAliment:NSTextAlignmentLeft onView:addressView];
    
    [self creatLabelFrame:CGRectMake(160, 20, WIDTH - 170, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"data_address"][@"phone"] andFont:15 andAliment:NSTextAlignmentRight onView:addressView];
    
    [self creatLabelFrame:CGRectMake(35, 60, WIDTH - 40, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"data_address"][@"address"] andFont:15 andAliment:NSTextAlignmentLeft onView:addressView];
    
    [heardView addSubview:addressView];
    
   
    [heardView addSubview:statisView];
    
    
    return heardView;
    }else {
        
        return nil;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    NSArray * array = self.dataDic[@"product"];
    
    if (section==array.count) {
        
        UIView * footerView = [[UIView alloc]initWithFrame:CGRectMake(0,0,WIDTH, 400)];
        footerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView * priceView =[[UIView alloc]initWithFrame:CGRectMake(0, 10, WIDTH, 50)];
    priceView.backgroundColor =[UIColor whiteColor];
    
    [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:[UIColor blackColor] andText:@"金额" andFont:15 andAliment:NSTextAlignmentLeft onView:priceView];
    
    [self creatLabelFrame:CGRectMake(130, 15, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"￥%@",self.dataDic[@"order_price"]] andFont:15 andAliment:NSTextAlignmentRight onView:priceView];
    
    [footerView addSubview:priceView];
    
    UIView * orderView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(priceView.frame)+10, WIDTH, 100)];
    orderView.backgroundColor =[UIColor whiteColor];
    
    [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:[UIColor blackColor] andText:@"订单编号" andFont:15 andAliment:NSTextAlignmentLeft onView:orderView];
    
    [self creatLabelFrame:CGRectMake(130, 15, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"order_sn"]] andFont:15 andAliment:NSTextAlignmentRight onView:orderView];
    
    [self creatLabelFrame:CGRectMake(10, 65, 120, 20) andTextColor:[UIColor blackColor] andText:@"订单变更时间" andFont:15 andAliment:NSTextAlignmentLeft onView:orderView];
    
    [self creatLabelFrame:CGRectMake(130, 65, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"update_time"]] andFont:15 andAliment:NSTextAlignmentRight onView:orderView];
    
    UIView * EMSView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(orderView.frame)+10, WIDTH, 100)];
    EMSView.backgroundColor =[UIColor whiteColor];
    
    [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:[UIColor blackColor] andText:@"快递公司" andFont:15 andAliment:NSTextAlignmentLeft onView:EMSView];
    
    [self creatLabelFrame:CGRectMake(130, 15, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"logistics_company"]] andFont:15 andAliment:NSTextAlignmentRight onView:EMSView];
    [self creatLabelFrame:CGRectMake(10, 65, 120, 20) andTextColor:[UIColor blackColor] andText:@"快递单号" andFont:15 andAliment:NSTextAlignmentLeft onView:EMSView];
    
    [self creatLabelFrame:CGRectMake(130, 65, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"logistics_number"]] andFont:15 andAliment:NSTextAlignmentRight onView:EMSView];
    [footerView addSubview:orderView];
    [footerView addSubview:EMSView];
    return footerView;
    }else{
        NSDictionary * dic = array[section];
        
        UIView * footerView = [[UIView alloc]initWithFrame:CGRectMake(0,0,WIDTH, 50)];
        footerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        
        UIView * commentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
        commentView.backgroundColor = [UIColor whiteColor];
        [footerView addSubview:commentView];
        
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)];
        label.text = @"评论内容:";
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:15];
        [commentView addSubview:label];
        
        UILabel * comment = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, WIDTH - 110, 20)];
        comment.text = dic[@"content"];
        comment.textColor = [UIColor blackColor];
        comment.font = [UIFont systemFontOfSize:15];
        comment.numberOfLines = 0;
        [comment sizeToFit];
        [commentView addSubview:comment];
        
        if (comment.text.length>0) {
              commentView .frame = CGRectMake(0, 0, WIDTH, CGRectGetMaxY(comment.frame)+10);
        }else {
            
              commentView .frame = CGRectMake(0, 0, WIDTH,40);
        }
   
        UIView *   replyView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(commentView.frame)+10, WIDTH, 50)];
        
        replyView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [footerView addSubview:replyView];
        
        UILabel * reply = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)];
        reply.text = @"平台回复:";
        reply.textColor = MyTabbarClick;
        reply.font = [UIFont systemFontOfSize:15];
        [replyView addSubview:reply];
        
        UILabel * replyVolu = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, WIDTH - 110, 20)];
        replyVolu.text = dic[@"replay"];
        replyVolu.textColor = [UIColor grayColor];
        replyVolu.font = [UIFont systemFontOfSize:15];
        replyVolu.numberOfLines = 0;
        [replyVolu sizeToFit];
        [replyView addSubview:replyVolu];
        
        
        if (replyVolu.text.length>0) {
            replyView.frame = CGRectMake(0, CGRectGetMaxY(commentView.frame)+10, WIDTH, CGRectGetMaxY(replyVolu.frame)+10);
        }else{
            
            replyView.frame = CGRectMake(0, CGRectGetMaxY(commentView.frame)+10, WIDTH, 40);
        }
    
        footerView.frame =  CGRectMake(0, 0, WIDTH, CGRectGetMaxY(replyView.frame));
        
        return footerView;
    }
}


-(void)creatLabelFrame:(CGRect)rec andTextColor:(UIColor*)col andText:(NSString *)text andFont:(CGFloat)flo andAliment:(NSTextAlignment)aliment onView:(UIView *)view {
    
    UILabel * statisLabel =[[UILabel alloc]initWithFrame:rec];
    statisLabel.font = [UIFont systemFontOfSize:flo];
    statisLabel.textColor = col;
    statisLabel.textAlignment = aliment;
    statisLabel.text = text;
    [view addSubview:statisLabel];
    
}

@end
